<?php
namespace Products\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;

class ProductsTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function addProducts($product,$image)
    {		
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){
			$userId=$_SESSION['user']['user_id'];
		}else{
			$userId='';
		}
		if(isset($product['quantity'])){
			$quantity=$product['qun'];
			$price=$product['price'];
			$content=null;
		}else if(isset($product['offer'])){
			$quantity=$product['oquantity'];
			$price=$product['oprice'];
			$content=$product['ocontent'];
		}
		$data = array(
			'category_id' 	     => $product['category'],
			'user_id'            => $userId,
			'product_name' 	     => $product['pname'], 	
			'type' 	             => $product['sales'], 	
			'country' 	         => $product['country'],	
			'quantity' 	         => $quantity,	
			'p_status' 	         => 0,	
			'added_date' 	     => date('Y-m-d H:i:s'),	
			'modified_date' 	 => date('Y-m-d H:i:s'),	
			'product_image' 	 => $image,	
			'product_content' 	 => $content,	
			'price'              => $price,  		
		);			
		$this->tableGateway->insert($data);		
		return $this->tableGateway->lastInsertValue;
    }
	public function updateProducts($product)
    {
		$data = array(
			'category_id' 	     => $product['category'],
			'user_id'            => $product['user_id'],
			'product_name' 	     => $product['pname'], 	
			'type' 	             => $product['sales'], 
			'country' 	         => $product['country'],			
			'quantity' 	         => $product['qun'],
			'p_status' 	         => 0,	
			'added_date' 	     => date('Y-m-d H:i:s'),	
			'modified_date' 	 => date('Y-m-d H:i:s'),	
			'product_image' 	 => $product['hidimages'],
			'product_content' 	 => null,	
			'price'              => $product['price'],  		
		);	
		$row=$this->tableGateway->update($data, array('user_id' => $product['user_id']));	return $row;	
	}
	public function getProducts($product_id){
		$select = $this->tableGateway->getSql()->select();
		$select->where('products.product_id="'.$product_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->current();
	}
	public function getallProducts($paginated=false){
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){
			$userid=$_SESSION['user']['user_id'];
		}else{
			$userid="";
		}
		$select = $this->tableGateway->getSql()->select();
		$select->join('users', 'products.user_id=users.user_id',array('*'),'left');		
		$select->join('offers', 'products.product_id=offers.product_id',array('offer_for_quantity','offer_price'),'left');		
		$select->where('products.user_id!="'.$userid.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		$paginatorAdapter = new DbSelect(
				$select,
				$this->tableGateway->getAdapter(),
				$resultSet
            );
		$paginator = new Paginator($paginatorAdapter);
            return $paginator;		
	}
	public function getallProducts1(){
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){
			$userid=$_SESSION['user']['user_id'];
		}else{
			$userid="";
		}
		$select = $this->tableGateway->getSql()->select();
		$select->join('users', 'products.user_id=users.user_id',array('*'),'left');		
		$select->join('offers', 'products.product_id=offers.product_id',array('offer_for_quantity','offer_price'),'left');$select->where('products.user_id="'.$userid.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;		
	}
	public function getallProducts2($data){
		$select = $this->tableGateway->getSql()->select();
		$select->join('users', 'products.user_id=users.user_id',array('*'),'left');		
		$select->join('offers', 'products.product_id=offers.product_id',array('offer_for_quantity','offer_price'),'left');	if($data=='0'){	
			$select->where('products.category_id!=""');
		}else{
			$select->where('products.category_id="'.$data.'"');
		}
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;		
	}
	public function getProductsGrid(){
		$select = $this->tableGateway->getSql()->select();
		$select->join('users', 'products.user_id=users.user_id',array('*'),'left');	
		$select->join('offers', 'products.product_id=offers.product_id',array('*'),'left');
		$select->where('products.product_id!=""');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->count();
	}
	public function getproGrid($aColumns,$data,$paginated=false){
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){
			$userid=$_SESSION['user']['user_id'];
		}else{
			$userid='';
		}
		$select = $this->tableGateway->getSql()->select();
		$select->join('users', 'products.user_id=users.user_id',array('*'),'left');	
		$select->join('offers', 'products.product_id=offers.product_id',array('oquantity' =>new Expression('offer_for_quantity'),'oprice' =>new Expression('offer_price')),'left');		
		$select->where('products.user_id="'.$userid.'"');
		// Search
			if(isset($data['sSearch'])){
				if($data['sSearch']!='0' && $data['sSearch']!='')
				{
					$select->where(array(
						new Predicate\PredicateSet(
							array(
							new Predicate\Like('product_name', '%'.$data['sSearch'].'%'),
							),
								Predicate\PredicateSet::COMBINED_BY_OR
						),
					));
				}
			}
		// End
		// Pagination
		if(isset($data['iDisplayStart']) && $data['iDisplayLength'] != '-1')
		{
			$start=(int)$data['iDisplayStart'];
			$currentPage = (int)$data['iDisplayLength'];
			$resultsPerPage = (int) $currentPage;
			$select->order('products.product_id DESC')->limit($currentPage)->offset($start);
		}
		// End
		
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	 }
	 public function deleteproducts($ids){		
		$deleteid=$this->tableGateway->delete(array('(product_id IN ('.$ids.'))'));
		return $deleteid;	
	}
}